<?php

class Article
{
    private $_db; // pdo 对象

    /**
     * 构造函数
     * @param PDO $_db
     */
    public function __construct($_db)
    {
        $this->_db = $_db;
    }


    /**
     * 新建文章
     * @param $title
     * @param $content
     * @param $userId
     * @return array
     * @throws Exception
     */
    public function create($title, $content, $userId)
    {
        if (empty($title)) {
            throw new Exception('文章标题不能为空', ErrorCode::ARTICLE_TITLE_CANNOT_EMPTY);
        }
        if (empty($content)) {
            throw new Exception('文章内容不能为空', ErrorCode::ARTICLE_CONTENT_CANNOT_EMPTY);
        }
        $sql = 'INSERT INTO `article` (`title`,`content`,`create_dt`,`user_id`) VALUES (:title,:content,:create_dt,:userId)';
        $createDt = time();
        $stmt = $this->_db->prepare($sql);
        $stmt->bindParam('title', $title);
        $stmt->bindParam('content', $content);
        $stmt->bindParam('create_dt', $createDt);
        $stmt->bindParam('userId', $userId);

        if (!$stmt->execute()) {
            throw new Exception('发表文章失败', ErrorCode::ARTICLE_CREATE_FEILED);
        }
        return [
            'article_id' => $this->_db->lastInsertId(),
            'title' => $title,
            'content' => $content,
            'user_id' => $userId,
            'create_dt' => $createDt
        ];

    }

    /**
     * 查看一篇文章
     * @param $articleId
     * @return mixed
     * @throws Exception
     */
    public function view($articleId)
    {
        if (empty($articleId)) {
            throw new Exception('文章ID不能为空', ErrorCode::ARTICLE_ID_CANNOT_EMPTY);
        }
        $sql = 'SELECT * FROM `article` WHERE `article_id` = :id';
        $stmt = $this->_db->prepare($sql);
        $stmt->bindParam('id',$articleId);
        $stmt->execute();
        $article = $stmt->fetch(PDO::FETCH_ASSOC);
        if (empty($article)){
            throw new Exception('文章为空',ErrorCode::ARTICLE_EMPTY);
        }
        return $article;
    }

    /**
     * 修改文章
     * @param $articleId
     * @param $title
     * @param $content
     * @param $userId
     */
    public function edit($articleId, $title, $content, $userId)
    {
        $article = $this->view($articleId);
        if ($article['user_id'] !== $userId){
            throw new Exception('你无权编辑文章',ErrorCode::EIDT_PERMISSION_DENIED);
        }
        $title = empty($title) ? $article['title'] : $title;
        $content = empty($content) ? $article['content'] : $content;
        if ($title === $article['title'] && $content === $article['content']){
            return $article;
        }
        $sql = 'UPDATE `article` SET `title`=:title, `content`=:content WHERE `article_id`=:id';
        $stmt = $this->_db->prepare($sql);
        $stmt->bindParam('title',$title);
        $stmt->bindParam('content',$content);
        $stmt->bindParam('id',$articleId);
        if (!$stmt->execute()){
            throw new Exception('文章编辑失败',ErrorCode::EIDT_ARTICLE_FEILED);
        }
        return [
            'article_id' => $articleId,
            'title' => $title,
            'content' => $content,
            'create_dt' => $article['create_dt']
        ];
    }

    /**
     * 删除文章
     * @param $articleId
     * @param $userId
     */
    public function delete($articleId, $userId)
    {
        $article = $this->view($articleId);
        if ($article['user_id'] !== $userId){
            throw new Exception('你无权删除文章',ErrorCode::DELETE_PERMISSION_DENIED);
        }
        $sql = 'DELETE FROM `article` WHERE `article_id`=:articleId AND `user_id`=:userId';
        $stmt = $this->_db->prepare($sql);
        $stmt->bindParam('articleId',$articleId);
        $stmt->bindParam('userId',$userId);
        if (false === $stmt->execute()){
            throw new Exception('删除失败',ErrorCode::DELETE_FEILED);
        }
        return true;
    }

    /**
     * 获取文章
     * @param $userId
     * @param int $page
     * @param int $size
     * @return array
     * @throws Exception
     */
    public function getList($userId, $page = 1, $size = 10)
    {
        if ($size > 100){
            throw new Exception('每页数据最多为100条',ErrorCode::SIZE_TOO_BIG);
        }
        $sql = 'SELECT * FROM `article` WHERE `user_id` = :userId LIMIT :start, :sizes';
        $start = ($page-1) * $size;
        $start = $start < 0 ? 0 : $start;
        $stmt = $this->_db->prepare($sql);
        $stmt->bindParam('userId',$userId);
        $stmt->bindParam('start',$start);
        $stmt->bindParam('sizes',$size);
        $stmt->execute();
        $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
        return $data;
    }
}